<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <!-- brands的数据遍历 -->
        <a
          href="javascript:;"
          @click="filterData.selectBrands = item.id"
          :class="{ active: item.id === filterData.selectBrands }"
          v-for="item in filterData.brands"
          :key="item.id"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <!-- saleProperties的数据遍历 -->
    <div class="item" v-for="sale in filterData.saleProperties" :key="sale.id">
      <div class="head">{{ sale.name }}:</div>
      <div class="body">
        <a
          href="javascript:;"
          @click="sale.selectProp = prop.id"
          :class="{ active: prop.id === sale.selectProp }"
          v-for="prop in sale.properties"
          :key="prop.id"
          >{{ prop.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from "vue";
import { findFliter } from "@/api/category";
import { useRoute } from "vue-router";
import xtxInfiniteLoadingVue from '@/components/library/xtx-infinite-loading.vue';
export default {
  name: "SubFilter",
  setup(props,{emit}) {
    const route = useRoute();
    // 记录品牌ID的
    const filterData = ref(null);
    const filterLoading = ref(null);
    // 根据当前路由获取数据 从无到有，后面都是数据的更新
    watch(
      () => route.params.id,
      (newVal) => {
        if (newVal && route.path === `/category/sub/${newVal}`) {
          filterLoading.value = true;
          findFliter(route.params.id).then((data) => {
            // filterData.value = data.result
            // 获取筛选数据
            // console.log(route.params.id, "11111");
            // 品牌
            // data.result.brands.selectBrands = null; //添加一个记录品牌id的
            data.result.selectBrands = null
            data.result.brands.unshift({ id: null, name: "全部" });
            // 规格
            data.result.saleProperties.forEach((p) => {
              p.selectProp = null; //给每一个p记录规格的id
              p.properties.unshift({ id: null, name: "全部" });
            });
            // console.log(data.result);
            filterData.value = data.result;
            // console.log(data.result,'data.result');
            // console.log(filterData.value,'filterData');
          });
          filterLoading.value = false;
        }
      },
      { immediate: true }
    )

    // 传参数的函数
    const getFilterParams = () => {
      // brandId参数与   attrs里面的groupName与propertyName
      let obj = {
        brandId:null,
        attrs:[]
      }
      // 将记录的id传进来
      obj.brandId = filterData.value.selectBrands
      // 传attrs的name
      filterData.value.saleProperties.forEach(p=>{
        const prop = p.properties.find(prop=>prop.id === p.selectProp)
        obj.attrs.push({groupName:p.name,propertyName:prop.name})
      })
      if(obj.attrs.length === 0) obj.attrs = null
      return obj
      // 需要{brandId:'',attrs:[{groupName:'',propertyName:''}]}
    }
    // 品牌筛选
    const changeBrand = (brandId) => {
      // 1.找到Id 如果第二次与第一次点击的相同,就不要请求数据了
      if(filterData.value.selectBrands === brandId) return 
        filterData.value.selectBrands = brandId
        // 触发事件
        emit('sort-change',getFilterParams())
    }
    // 销售属性筛选
    const changeProp = (sale,propId) => {
      if(sale.selectProp === propId) return 
        sale.selectProp=propId
        emit('sort-change',getFilterParams())
    }

   
    // changeBrand(item.id)   changeProp(sale,prop.id)
    return { filterData,filterLoading,changeProp, changeBrand}
  },
};
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.item {
  padding: 10px 0;
}
</style>